package com.example.sqlite;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.utilitaire.Stat_Boutique;

public class DM_Society_Boutique 
{

	private SQLiteDatabase _myDB;
	private MySQliteCreator_Boutique _mySqliteCreator_Boutique;

	public DM_Society_Boutique(Context pContext)
	{
		_mySqliteCreator_Boutique = new MySQliteCreator_Boutique(pContext,Stat_Boutique.DB_NAME, null, 1);
		openDB();
	}

	public void openDB()
	{
		_myDB = _mySqliteCreator_Boutique.getWritableDatabase();
	}

	//
	public ArrayList<HashMap<String, String>> rechercheArticle(String constructeur) 
	{

		Cursor c = _myDB.rawQuery("SELECT * FROM  tab_articles WHERE  constructeur = '"+ constructeur + "'", null);

		ArrayList<HashMap<String, String>> lArrayList = new ArrayList<HashMap<String, String>>();

		HashMap<String, String> lHashMap;

		if (c.getCount() != 0)
		{
			while (c.moveToNext())
			{

				String lRef = c.getString(c
						.getColumnIndex(Stat_Boutique.COL_REF));
				String lConst = c.getString(c
						.getColumnIndex(Stat_Boutique.COL_CONST));
				double lPrix = c.getDouble(c
						.getColumnIndex(Stat_Boutique.COL_PRIX));
				String lCaract = c.getString(c
						.getColumnIndex(Stat_Boutique.COL_CARACT));

				lHashMap = new HashMap<String, String>();

				lHashMap.put("KEY_REF", lRef);
				lHashMap.put("KEY_CONST", lConst);
				lHashMap.put("KEY_PIX", String.valueOf(lPrix));
				lHashMap.put("KEY_CARACT", lCaract);

				lArrayList.add(lHashMap);
			}
		}

		return lArrayList;

	}

	public void insert(Society_Article pSociety_Article)
	{
		ContentValues lValue = new ContentValues();

		lValue.put(Stat_Boutique.COL_REF, pSociety_Article.getReference());
		lValue.put(Stat_Boutique.COL_CONST, pSociety_Article.getConstructeur());
		lValue.put(Stat_Boutique.COL_PRIX, pSociety_Article.getPrix());
		lValue.put(Stat_Boutique.COL_CARACT,
				pSociety_Article.getCaracteristiques());

		_myDB.insert(Stat_Boutique.TABLE_NAME, null, lValue);
	}

	public void clearAll() 
	{

		_myDB.execSQL("DELETE FROM " + Stat_Boutique.TABLE_NAME);

	}

	public Cursor afficherTout() 
	{
		return _myDB.rawQuery("SELECT * FROM tab_articles", null);
	}

	/*
	 * public ArrayList<Society_Article> getListSocietyFromCursor(Cursor c)
	 *  {
	 * ArrayList<Society_Article> lArray=new ArrayList<Society_Article>();
	 * 
	 * if(c.getCount()!=0) { while(c.moveToNext()) { int
	 * lID=c.getInt(c.getColumnIndex(Stat_Boutique.COL_ID)); String lRef=
	 * c.getString(c.getColumnIndex(Stat_Boutique.COL_REF)); String lConst=
	 * c.getString(c.getColumnIndex(Stat_Boutique.COL_CONST)); double lPrix=
	 * c.getDouble(c.getColumnIndex(Stat_Boutique.COL_PRIX)); String lCaract=
	 * c.getString(c.getColumnIndex(Stat_Boutique.COL_CARACT));
	 * 
	 * Society_Article s=new Society_Article(lID, lRef, lConst, lPrix, lCaract);
	 * lArray.add(s); } }
	 * 
	 * return lArray; }
	 */

	public Cursor afficherTout_panier() 
	{
		return _myDB.rawQuery("SELECT * FROM tab_panier", null);

	}

	/*
	 * public ArrayList<Society_Panier> getListSocietyFromCursor_panier(Cursor
	 * c) { ArrayList<Society_Panier> lArrayList = new
	 * ArrayList<Society_Panier>(); if(c.getCount()!=0) { while(c.moveToNext())
	 * { int ID=c.getInt(c.getColumnIndex(Stat_Boutique.COL_P_ID)); String
	 * lRef=c.getString(c.getColumnIndex(Stat_Boutique.COL_P_REF)); double
	 * lPrix=c.getDouble(c.getColumnIndex(Stat_Boutique.COL_P_PRIX)); int
	 * lQuantity=c.getInt(c.getColumnIndex(Stat_Boutique.COL_P_QUANTITY));
	 * 
	 * Society_Panier s = new Society_Panier(ID, lRef, lPrix, lQuantity);
	 * lArrayList.add(s); } } return lArrayList; }
	 */

	public void insert_panier(Society_Panier pSociety_Panier)
	{
		ContentValues lValues = new ContentValues();

		lValues.put(Stat_Boutique.COL_P_REF, pSociety_Panier.getReference());
		lValues.put(Stat_Boutique.COL_P_PRIX, pSociety_Panier.getPrix());
		lValues.put(Stat_Boutique.COL_P_QUANTITY, pSociety_Panier.getQuantity());

		_myDB.insert(Stat_Boutique.TABLE_NAME_P, null, lValues);
	}

	public void clearAll_panier() 
	{
		_myDB.execSQL("DELETE FROM " + Stat_Boutique.TABLE_NAME_P);
	}

	public void closeDB() 
	{
		_myDB.close();
	}

}
